//
//  NSString+Category.m
//  AiCheSong
//
//  Created by 李华 on 2017/11/30.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "NSString+Category.h"
#import<CommonCrypto/CommonDigest.h>
@implementation NSString (Category)
/**
 *  动态获取文字长度
 *
 *  @param font    该文本的字体
 */
- (CGSize)getSizeWithFont:(UIFont *)font {
    return [self getSizeWithFont:font maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
}
/**
 *  动态获取文字长度
 *
 *  @param font    该文本的字体
 *  @param maxSize 该文本的尺寸(填写最大值)
 */
- (CGSize)getSizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
/**
 *  小写字母转大写
 */
- (NSString *)changeLetter {
    NSArray *arrayL = @[@"a",@"b",@"c",@"d",@"e",@"f"];
    NSArray *arrayB = @[@"A",@"B",@"C",@"D",@"E",@"F"];
    __block NSString *string = @"";
    for (NSInteger i = 0; i < self.length; i ++) {
        NSString *letter = [self substringWithRange:NSMakeRange(i, 1)];
        __block NSInteger place = -1;
        [arrayL enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:letter]) {
                place = idx;
                *stop = YES;
            }
        }];
        if (place != -1) {
            letter = arrayB[place];
        }
        string = [string stringByAppendingString:letter];
    }
    return string;
}
/**
 *  判断是否符合正则表达式规则
 *
 *  @pram regex  需要判断的正则表达式
 */
- (BOOL)isValidateRegex:(NSString *)regex {
    if (regex) {
        NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isVali = [test evaluateWithObject:self];
        return isVali;
    }
    return YES;
}
- (unsigned long long)fileSize {
    // 总大小
    unsigned long long size = 0;
    if ([self containsString:BaseInfo.sendBox]) {
        // 文件管理者
        NSFileManager *mgr = [NSFileManager defaultManager];
        
        // 文件属性
        NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
        
        if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹
            // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
            NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
            for (NSString *subpath in enumerator) {
                // 全路径
                NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
                // 累加文件大小
                size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
            }
        } else { // 文件
            size = attrs.fileSize;
        }
        
    }
    return size;
}
- (unsigned long long)fileFolderSize {
    // 总大小
    unsigned long long size = 0;
    if ([self containsString:BaseInfo.sendBox]) {
        // 文件管理者
        NSFileManager *mgr = [NSFileManager defaultManager];
        
        // 是否为文件夹
        BOOL isDirectory = NO;
        
        // 路径是否存在
        BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
        if (!exists) return size;
        
        if (isDirectory) { // 文件夹
            // 获得文件夹的大小  == 获得文件夹中所有文件的总大小
            NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
            for (NSString *subpath in enumerator) {
                // 全路径
                NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
                // 累加文件大小
                size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
            }
        } else { // 文件
            size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
        }
    }
    return size;
}
/**
 *  MD5加密
 */
- (NSString *)MD5 {
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (unsigned)strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
/**
 *  MD5加盐加密
 */
- (NSString *)MD5WithSalt {
    const char *cStr = [[TYZMD5SALT stringByAppendingString:self] UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (unsigned)strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        NSString *str = [NSString stringWithFormat:@"%02x", digest[i]];
        [output appendString:str];
    }
    return output;
}
/**
 *  URL中的中文字符转换
 */
- (NSString *)URLString {
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

#pragma mark - 32位 小写
+(NSString *)MD5ForLower32Bate:(NSString *)str{
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    return digest;
}
#pragma mark - 32位 大写
+ (NSString *)MD5ForUpper32Bate:(NSString *)str{
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02X", result[i]];
    }
    
    return digest;
}
#pragma mark - 16位 大写
+ (NSString *)MD5ForUpper16Bate:(NSString *)str{
    NSString *md5Str = [self MD5ForUpper32Bate:str];
    NSString *string;
    for (int i=0; i<24; i++) {
        string=[md5Str substringWithRange:NSMakeRange(8, 16)];
    }
    return string;
}
#pragma mark - 16位 小写
+ (NSString *)MD5ForLower16Bate:(NSString *)str{
    NSString *md5Str = [self MD5ForLower32Bate:str];
    NSString *string;
    for (int i=0; i<24; i++) {
        string=[md5Str substringWithRange:NSMakeRange(8, 16)];
    }
    return string;
}
@end
